package cn.xi.dao;

import cn.xi.pojo.CheckGroup;
import cn.xi.pojo.CheckItem;
import cn.xi.pojo.Setmeal;
import com.github.pagehelper.Page;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

public interface SetmealDao {
    @Select("select * from t_setmeal")
    Page<Setmeal> pages();

    @Select("select * from t_setmeal where code like concat('%',#{queryString},'%') or name like concat('%',#{queryString},'%')")
    Page<Setmeal> pagesString(String queryString);

    @Select("select * from t_checkgroup")
    List<CheckGroup> findall();

    @Insert("insert into t_setmeal (name,code,helpCode,sex,age,price,remark,attention,img) values(#{name},#{code},#{helpCode},#{sex},#{age},#{price},#{remark},#{attention},#{img})")
    void save(Setmeal setmeal);

    @Select("select LAST_INSERT_ID()")
    Integer getid();

    @Insert("insert into t_setmeal_checkgroup values (#{sid},#{gid})")
    void savesetmeaandgroup(@Param(value = "sid") Integer i, @Param(value = "gid") Integer integer);

    @Select("select * from t_setmeal where id =#{id}")
    Setmeal selectsetmeal(Integer id);

    @Select("select checkgroup_id from t_setmeal_checkgroup where setmeal_id=#{id}")
    Integer[] selectSetmealAndGroupDao(Integer id);

    @Update("update t_setmeal set name=#{name},code=#{code},helpCode=#{helpCode},sex=#{sex},age=#{age},price=#{price},remark=#{remark},attention=#{attention},img=#{img} where id =#{id}")
    void update(Setmeal setmeal);

    @Delete("delete from t_setmeal_checkgroup where setmeal_id=#{id}")
    void deleteSetmeal(Integer id);

    @Delete("delete from t_setmeal where id=#{id}")
    void delete(Integer id);

    @Select("select img from t_setmeal where id =#{id}")
    String selecteImg(Integer id);

    @Select("select * from t_setmeal where id = #{id}")
    @Results({
            @Result(id = true, property = "id", column = "id"),
            @Result(property = "name", column = "name"),
            @Result(property = "code", column = "code"),
            @Result(property = "helpCode", column = "helpCode"),
            @Result(property = "sex", column = "sex"),
            @Result(property = "age", column = "age"),
            @Result(property = "price", column = "price"),
            @Result(property = "remark", column = "remark"),
            @Result(property = "attention", column = "attention"),
            @Result(property = "img", column = "img"),
            @Result(property = "checkGroups", column = "id",
                    javaType = List.class,
                    many = @Many(select = "cn.xi.dao.SetmealDao.selectCheckGroup")),
    })
    Setmeal findById(Integer id);

    @Select("select * from t_setmeal")
    @Results({
            @Result(id = true, property = "id", column = "id"),
            @Result(property = "name", column = "name"),
            @Result(property = "code", column = "code"),
            @Result(property = "helpCode", column = "helpCode"),
            @Result(property = "sex", column = "sex"),
            @Result(property = "age", column = "age"),
            @Result(property = "price", column = "price"),
            @Result(property = "remark", column = "remark"),
            @Result(property = "attention", column = "attention"),
            @Result(property = "img", column = "img"),
            @Result(property = "checkGroups", column = "id",
                    javaType = List.class,
                    many = @Many(select = "cn.xi.dao.SetmealDao.selectCheckGroup")),
    })
    List<Setmeal> findBy();

    @Select("select * from t_checkgroup g,t_setmeal_checkgroup sg where g.id=sg.checkgroup_id and sg.setmeal_id=#{id}")
    @Results({
            @Result(id = true, property = "id", column = "id"),
            @Result(property = "code", column = "code"),
            @Result(property = "name", column = "name"),
            @Result(property = "helpCode", column = "helpCode"),
            @Result(property = "sex", column = "sex"),
            @Result(property = "remark", column = "remark"),
            @Result(property = "attention", column = "attention"),
            @Result(property = "checkItems", column = "id",
                    javaType = List.class,
                    many = @Many(select = "cn.xi.dao.SetmealDao.selectCheckItem")
            ),
    })
    List<CheckGroup> selectCheckGroup(Integer id);


    @Select("select i.* from t_checkgroup_checkitem gi, t_checkitem i where i.id=gi.checkitem_id and gi.checkgroup_id=#{id}")
    List<CheckItem> selectCheckItem(Integer id);


    @Select("select s.name,COUNT(o.id) value from t_order o ,t_setmeal s where o.setmeal_id = s.id group by s.name")
    List<Map> selectandorder();

}
